package com.store.practice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.store.practice.mapper.*;
import com.store.practice.model.*;
import com.store.practice.service.StaffManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class StaffManageServiceImpl implements StaffManageService {
    @Autowired
    private BgStoreStaffMapper staffMapper;
    @Autowired
    private BgStoreMapper storeMapper;
    @Autowired
    private BgStorestaffSkilltagRelationMapper relationMapper;
    @Autowired
    private BgSkillTagMapper tagMapper;
    @Autowired
    private AppAccountMapper accountMapper;

    @Override
    public List<Map> list(Integer storeId,Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        BgStoreStaffExample example = new BgStoreStaffExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        List<BgStoreStaff> staffs = staffMapper.selectByExample(example);
        ArrayList<Map> result = new ArrayList<>();
        if (staffs==null){
            return null;
        }
        for (BgStoreStaff staff:staffs){
            HashMap<String, Object> map = new HashMap<>();
            map.put("header",staff.getPicture());
            map.put("name",staff.getName());
            map.put("position",staff.getPosition());
            map.put("mobileNum",staff.getMobileNum());
            map.put("staffId",staff.getId());
            List<Integer> ids = relationMapper.getTagIdsByStaffId(staff.getId());
            ArrayList<String> tags = new ArrayList<>();
            if (ids!=null){
                for (Integer id:ids){
                    String tag = tagMapper.selectByPrimaryKey(id).getTag();
                    tags.add(tag);
                }
            }
            tags.add(staff.getStatus()==0?"已离职":null);
            map.put("skill",tags);
            result.add(map);
        }
        return result;
    }

    @Override
    public Map getStaff(Integer id) {
        BgStoreStaff staff = staffMapper.selectByPrimaryKey(id);
        Map<String, Object> result = BeanUtil.beanToMap(staff);
        result.put("storeName",storeMapper.selectByPrimaryKey(staff.getStoreId()).getName());
        List<Integer> ids = relationMapper.getTagIdsByStaffId(staff.getId());
        ArrayList<String> tags = new ArrayList<>();
        if (ids!=null){
            for (Integer tagId:ids){
                String tag = tagMapper.selectByPrimaryKey(tagId).getTag();
                tags.add(tag);
            }
        }
        tags.add(staff.getStatus()==0?"已离职":null);
        result.put("skill",tags);
        return result;
    }

    @Override
    public int updateWorkStatus(Integer id, Integer param) {
        BgStoreStaff staff = staffMapper.selectByPrimaryKey(id);
        if (param==0){
            staff.setStatus(0);
            AppAccount account = accountMapper.selectByPrimaryKey(staff.getAccountId());
            account.setStatus(0);
            accountMapper.updateByPrimaryKey(account);
        }
        if (param==1){
            staff.setStatus(1);
            AppAccount account = accountMapper.selectByPrimaryKey(staff.getAccountId());
            account.setStatus(1);
            accountMapper.updateByPrimaryKey(account);
        }
        return staffMapper.updateByPrimaryKey(staff);
    }
}
